package com.cheeven.amazonbooksreviewscores;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class GetBooksFromTop100Results {

    GetPage page = new GetPage();

    public static void main(String[] args) throws Exception {

        String sourceFile = "top100.txt";
        String outputFile = "top100-details.html";
        if (args.length > 0) {
            sourceFile = args[0];
        }
        if (args.length > 1) {
            outputFile = args[1];
        }

        // load book info
        File ISBNList = new File(sourceFile);
        final ArrayList<HashMap<String, String>> bookInfos = new ArrayList<HashMap<String, String>>();
        BufferedReader in = new BufferedReader(new FileReader(ISBNList));
        String data = in.readLine();
        while (data != null) {
            data = data.trim();
            if (data.length() > 0 && !data.startsWith("#")) {
                String[] dataArray = data.split("\t");
                String rank = dataArray[0];
                String asin = dataArray[1];
                String title = dataArray[2];
                String urlString = dataArray[3];
                if (urlString.startsWith("/")) {
                    urlString = "http://www.amazon.com" + urlString;
                }
                //System.out.println(urlString);
                HashMap<String, String> bookInfo = new HashMap<String, String>();
                bookInfo.put("rank", rank);
                bookInfo.put("asin", asin);
                bookInfo.put("title", title);
                bookInfo.put("urlString", urlString);
                bookInfos.add(bookInfo);
            }
            data = in.readLine();
        }
        in.close();
        System.out.println("Loaded " + bookInfos.size() + " books.");

        // prepare the output file
        File scoreList = new File(outputFile);
        PrintWriter out = new PrintWriter(new FileWriter(scoreList));
        out.println(Top100ReportTemplate.HEADER);
        
        // start download each book
        GetSingleBookByURL scores = new GetSingleBookByURL();
        for (int i = 0; i < bookInfos.size(); i++) {
            HashMap<String, String> bookInfo = bookInfos.get(i);
            System.out.println("Get Book (" + i + ") " + bookInfo);
            Map<String, String> bookInfo2 = scores.findBook(bookInfo.get("urlString"));
            bookInfo2.putAll(bookInfo);
            Top100ReportTemplate.doBody(bookInfo2, out);
            
            // flush the output every 10 records to reduce the disk I/O overhead
            // also try to release memory
            if(i%10==0 || i==bookInfos.size()) {
                out.flush();
                System.gc();
            }
        }

        // close the output file
        out.println(Top100ReportTemplate.FOOTER);
        out.close();
    }
}
